/**
 * Copyright 2024 Inc.
 **/
package com.myz.java.study.juc;

import com.myz.java.study.juc.thread.threadpool.NamedThreadFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author maoyz0621 on 2024/4/25
 * @version v1.0
 */
public class DelayRegistry {

    private final BlockingQueue<DelayContext> queue = new LinkedBlockingQueue<>(1024);

    public void start() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("delay-publish"));
        executor.submit(new DelayConsumer(queue));
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                DelayContext delayContext = new DelayContext("delay-publish", 1, 1);
                publish(delayContext);
            }
        }).start();
    }

    public void publish(DelayContext delayContext) {
        queue.add(delayContext);
    }

}